﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;

namespace AnimEditor.Define
{
  public class AnimPoint : Object
  {
    int mX;
    [Category("属性"), Description("X")]
    public int X
    {
      get { return mX; }
      set { mX = value; }
    }

    int mY;
    [Category("属性"), Description("Y")]
    public int Y
    {
      get { return mY; }
      set { mY = value; }
    }
    public AnimPoint()
    {
      mX = 0;
      mY = 0;
    }
    public AnimPoint(int x, int y)
    {
      mX = x;
      mY = y;
    }
  }

  public class AnimSize : Object
  {
    int mWidth;
    [Category("属性"), Description("Width")]
    public int Width
    {
      get { return mWidth; }
      set { mWidth = value; }
    }
    int mHeight;
    [Category("属性"), Description("Height")]
    public int Height
    {
      get { return mHeight; }
      set { mHeight = value; }
    }

    public AnimSize()
    {
      mWidth = 0;
      mHeight = 0;
    }

    public AnimSize(int x, int y)
    {
      mWidth = x;
      mHeight = y;
    }
  }

  public class PropertyMgr
  {
    /// <summary>
    /// 判断属性传入的值是否合法。如果不合法，则修改为默认值
    /// </summary>
    /// <param name="value">传入的值</param>
    /// <param name="shouldNum">值解析后该有的个数</param>
    /// <param name="defaultProperty">默认值</param>
    /// <returns></returns>
    static public bool IsValid(ref string value, int shouldNum, string defaultProperty)
    {
      string[] property = value.Split(',');
      if(!(value is string) || property.Length != shouldNum)
      {
        value = defaultProperty;
        return false;
      }

      value = "";
      for (int i = 0; i < property.Length; i++)
      {
        try
        {
          int temp = int.Parse(property[i]);
          if (i == 0)
            value += temp.ToString();
          else
            value += ',' + temp.ToString();
        }
        catch (System.FormatException ex)
        {
          value = defaultProperty;
          System.Console.WriteLine(ex.ToString());
          return false;
        }      
      }
      return true;
    }

    static public AnimPoint StringToPoint(string value)
    {
      string[] property = value.Split(',');
      if(property.Length < 2)
      {
        return null;
      }

      try
      { 
        return new AnimPoint(int.Parse(property[0]), int.Parse(property[1]));
      }
      catch (System.FormatException ex)
      {
        System.Console.WriteLine(ex.ToString());
        return null;
      }    
    }

	static public string PointToString(AnimPoint point)
	{
		return string.Format("{0},{1}", point.X, point.Y);
	}

    static public AnimSize StringToSize(string value)
    {
      string[] property = value.Split(',');
      if (property.Length < 2)
      {
        return null;
      }

      try
      {
        return new AnimSize(int.Parse(property[0]), int.Parse(property[1]));
      }
      catch (System.FormatException ex)
      {
        System.Console.WriteLine(ex.ToString());
        return null;
      }
    }
	static public string SizeToString(AnimSize value)
	{
		return string.Format("{0},{1}", value.Width, value.Height);
	}
  }

  public class AnimRect : Object
  {
    AnimPoint mPoint = new AnimPoint();
    AnimSize mSize = new AnimSize();

    public AnimRect()
      : this(0, 0, 0, 0)
    {
    }
    public AnimRect(AnimRect rect)
    {
      mPoint.X = rect.Point.X;
      mPoint.Y = rect.Point.Y;
      mSize.Width = rect.Size.Width;
      mSize.Height = rect.Size.Height;
    }
    public AnimRect(int x, int y, int w, int h)
    {
      mPoint.X = x;
      mPoint.Y = y;
      mSize.Width = w;
      mSize.Height = h;
    }
    public void SetAnimRect(int x, int y, int w, int h)
    {
      mPoint.X = x;
      mPoint.Y = y;
      mSize.Width = w;
      mSize.Height = h;
    }
    public Rectangle ToRectangle()
    {
      return new Rectangle(mPoint.X, mPoint.Y, mSize.Width, mSize.Height);
    }
    [Category("属性"), Description("Point")]
    public AnimPoint Point
    {
      get { return mPoint; }
      set { mPoint = value; }
    }
     [Category("属性"), Description("Size")]
    public AnimSize Size
    {
      get { return mSize; }
      set { mSize = value; }
    }
    public bool IsInRect(AnimRect rect)
    {
      if (Point.X < rect.Point.X || Point.X > rect.Point.X + rect.Size.Width)
        return false;
      if (Point.Y < rect.Point.Y || Point.Y > rect.Point.Y + rect.Size.Height)
        return false;
      if (Point.X + Size.Width < rect.Point.X || Point.X + Size.Width > rect.Point.X + rect.Size.Width)
        return false;
      if (Point.Y + Size.Height < rect.Point.Y || Point.Y + Size.Height > rect.Point.Y + rect.Size.Height)
        return false;

      return true;
    }
    public void Copy(AnimRect rect)
    {
      mPoint.X = rect.Point.X;
      mPoint.Y = rect.Point.Y;
      mSize.Width = rect.Size.Width;
      mSize.Height = rect.Size.Height;
    }
  }
}
